<?php

/**
 * @author g.antoniadis (george@noodles.gr)
 * @copyright 2008
 *
 * array_blend() gets two arrays and recursively combines them together, 
 * overwriting the values of the first array where the same key exists
 * in the second and sorts resulting array.
 * 
 * used mostly for configuration arrays in order to overwrite values.
 * php's array_merge and array_merge_recursive simply append the values 
 * of the later array and do not produce the wanted result.
 *
 * based on the code of Keith Devens
 * http://keithdevens.com/weblog/archive/2003/Jul/10/MergeTwoArraysV2
 *
 * @param mixed $a
 * @param mixed $b
 * @return
 */
 
function array_blend( $a, $b )
{
	if( is_array( $a ) and is_array( $b ) )
	{
		while( list( $key ) = each( $b ) )
		{
			if( array_key_exists( $key, $a ) )
			{
				$a[$key] = merge( $a[$key], $b[$key] );
			}
			else
			{
				$a[$key] = $b[$key];
			}
		}
	}
	else
	{
		$a = $b;
	}
	
	if ( is_array( $a ) )
	{
		ksort( $a );
	}
	
	return $a;
}

?>